package com.exchange.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.exchange.aspect.logger.SysLogger;
import com.exchange.aspect.user.CheckLogin;
import com.exchange.user.dto.SelUserDto;
import com.exchange.user.dto.UserLoginDto;
import com.exchange.user.entity.SysUser;
import com.exchange.group.SelectGroup;
import com.exchange.user.service.SysUserService;
import com.exchange.utils.result.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/***
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 *
 * @Author liuhai
 * @Date 2020-11-12 09:32:37
 * @Description
 * @RefreshScope 动态刷新获取config,config的注解
 */
@RestController
@RefreshScope
@RequestMapping("/system/user")
@Api(value = "用户管理", tags = "用户管理接口")
public class UserController {

    @Value("${server.port}")
    private Integer port;
    @Value("${params.name}")
    private String envName;

    @CheckLogin
    @GetMapping("/hello")
    public CommonResult hello(@RequestParam String name) {
        return CommonResult.success("hello " + name + ", port:" + port + ", envName " + envName);
    }

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/userLogin")
    @ApiOperation("用户登陆")
    @SysLogger(desc = "用户登陆", type = "user-01")
    public CommonResult userLogin(@RequestBody @Validated(value = SelectGroup.class) UserLoginDto userLoginDto){
        if(null == userLoginDto || StringUtils.isEmpty(userLoginDto.getUserName()) || StringUtils.isEmpty(userLoginDto.getPassword())){
            return CommonResult.fail("参数不能为空，用户名和密码必填");
        }
        String userToken = sysUserService.userLogin(userLoginDto);
        if(StringUtils.isEmpty(userToken)){
            return CommonResult.fail("用户名或密码有误");
        }
        SysUser sysUser = sysUserService.getUserByToken(userToken);
        if(null == sysUser){
            return CommonResult.fail("获取用户信息失败");
        }
        return CommonResult.successWithData("登陆成功", sysUser);
    }

    @GetMapping("/getUserByToken")
    @ApiOperation("根据token获取用户信息")
    @SysLogger(desc = "根据token获取用户信息", type = "user-02")
    @CheckLogin
    public CommonResult getUserByToken(@RequestParam("token") String token){
        SysUser sysUser = sysUserService.getUserByToken(token);
        if(null == sysUser){
            return CommonResult.fail("token无效，获取用户信息失败");
        }
        return CommonResult.successWithData("获取用户信息成功", sysUser);
    }

    @PostMapping("/getUserList")
    @ApiOperation("查询用户列表")
    @SysLogger(desc = "查询用户列表", type = "user-03")
    @CheckLogin
    public CommonResult getUserList(@RequestBody @Validated(value = SelectGroup.class) SelUserDto selUserDto){
        IPage<SysUser> userIPage = sysUserService.getPageList(selUserDto);
        if(null == userIPage){
            return CommonResult.fail("获取用户列表失败");
        }
        return CommonResult.successWithData("获取用户列表成功", userIPage);
    }

    @GetMapping("/getUserById")
    @ApiOperation("根据用户ID查询用户信息")
    @SysLogger(desc = "根据用户ID查询用户信息", type = "user-04")
    @CheckLogin
    public CommonResult getUserById(@RequestParam("userId") Integer userId){
        SysUser sysUser = sysUserService.getUserById(userId);
        if(null == sysUser){
            return CommonResult.fail("获取用户信息失败");
        }
        return CommonResult.successWithData("获取用户信息成功", sysUser);
    }

}
